home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / p063b9s.zip / UNIT / UNIXDATE.PAS < prev    next >
Pascal/Delphi Source File  |  1996-04-20  |  3KB  |  104 lines

  1. UNIT unixdate;
  2. { Unix format (secs since 1/1-1970) <=> YYMMDDHHMMSS conversion    }
  3. { Based on PibTerm source               04-may-88 Kristian Ottosen }
  4. {                                                                  }
  5. {$I POPDEFS.INC}
  6.  
  7. INTERFACE
  8.  
  9. USES Use32;
  10.  
  11. FUNCTION GetUnixDate(Year, Month, Day, Hour, Mins, Secs : Word) : LongInt;
  12. PROCEDURE UnpackUnix(Date           : LongInt;
  13.                      VAR Year       : Word;
  14.                      VAR Month      : Word;
  15.                      VAR Day        : Word;
  16.                      VAR Hour       : Word;
  17.                      VAR Mins       : Word;
  18.                      VAR Secs       : Word);
  19.  
  20. IMPLEMENTATION
  21.  
  22. {$IFDEF DateDebug}
  23. USES LogFile, OpString;
  24. {$ENDIF}
  25.  
  26. CONST
  27.   DaysPerMonth   : ARRAY[1..12] OF Byte = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  28.   SecsPerYear     = 31536000;
  29.   SecsPerLeapYear = 31622400;
  30.   SecsPerDay      = 86400;
  31.   SecsPerHour     = 3600;
  32.   SecsPerMinute   = 60;
  33.  
  34.   FUNCTION GetUnixDate;
  35.   VAR
  36.     Date : LongInt;
  37.     t    : LongInt;
  38.     i    : Word;
  39.   BEGIN
  40.     Date := 0;
  41.     FOR i := 1970 TO (Year - 1) DO
  42.     BEGIN
  43.       IF (i MOD 4) = 0 THEN
  44.         t := SecsPerLeapYear
  45.       ELSE
  46.         t := SecsPerYear;
  47.       Inc(Date, t);
  48.     END;
  49.     IF (Year MOD 4) = 0 THEN
  50.       DaysPerMonth[2] := 29
  51.     ELSE
  52.       DaysPerMonth[2] := 28;
  53.     FOR i := 1 TO (Month - 1) DO
  54.       Inc(Date, LongInt(DaysPerMonth[i] * SecsPerDay));
  55.     Inc(Date, LongInt(LongInt(Day-1)*SecsPerDay+LongInt(Hour)*SecsPerHour+LongInt(Mins)*SecsPerMinute+Secs));
  56.     GetUnixDate := Date;
  57.   END;
  58.  
  59.   PROCEDURE UnpackUnix;
  60.   VAR
  61.     RDate : LongInt;
  62.     t     : LongInt;
  63.   BEGIN
  64.     Year := 1970;
  65.     Month := 1;
  66.     RDate := Date;
  67.     t:=0;
  68.     WHILE (RDate > 0) DO
  69.     BEGIN
  70.       IF (Year MOD 4) = 0 THEN
  71.         t := SecsPerLeapYear
  72.       ELSE
  73.         t := SecsPerYear;
  74.       Dec(RDate, t);
  75.       Inc(Year);
  76.     END;
  77.     Inc(RDate, t);
  78.     Dec(Year);
  79.     IF (Year MOD 4) = 0 THEN
  80.       DaysPerMonth[2] := 29
  81.     ELSE
  82.       DaysPerMonth[2] := 28;
  83.     t:=0;
  84.     WHILE (RDate > 0) DO
  85.     BEGIN
  86.       t := LongInt(DaysPerMonth[Month]) * SecsPerDay;
  87.       Dec(RDate, t);
  88.       Inc(Month);
  89.     END;
  90.     Inc(RDate, t);
  91.     Dec(Month);
  92.     Day := LongInt(RDate + SecsPerDay - 1) DIV SecsPerDay;
  93.     Dec(RDate, ((Day-1) * SecsPerDay));
  94.     Hour := RDate DIV SecsPerHour;
  95.     Dec(RDate, (LONGINT(Hour) * LONGINT(SecsPerHour)));
  96.     Mins := RDate DIV SecsPerMinute;
  97.     Secs := RDate - (Mins * SecsPerMinute);
  98. {$IFDEF DateDebug}
  99. if Year>1994 then addlog('!','Unixdate error: '+Long2str(Date)+' = '+Long2str(day)+'/'+long2str(month)+'-'+long2str(year));
  100. {$ENDIF}
  101.   END;
  102.  
  103. END.
  104.